/** m2settings is a GUI to configure Maven 2 settings.xml 
 * 
 *  Copyright (C) 2007 Regis Decamps <decamps@users.sf.net>
 * 
 *  This file is part of m2settings.
 *
 *  m2settings is free software; you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation; either version 2 of the License, or
 *  (at your option) any later version. 
 *
 *  Foobar is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with Foobar; if not, write to the Free Software
 *  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
 *  or read it online http://www.gnu.org/licenses/gpl.txt
 *
 **/
/*
 * JFrameProxy.java
 *
 * Created on April 25, 2007, 2:14 PM
 */

package info.decamps.m2settings.swing;

import info.decamps.m2settings.beans.MyProxy;
import info.decamps.m2settings.documentation.DocumentationProvider;
import info.decamps.m2settings.model.Proxy;
import info.decamps.m2settings.model.ProxyTableModel;
import javax.swing.JSpinner;
import javax.swing.SpinnerNumberModel;
import org.apache.log4j.Logger;


/**
 *
 * @author  regis
 */
public class JFrameProxy extends javax.swing.JFrame {
    private static final String[] PROTOCOLS={"http","ftp","https","ftps"};
    private Logger log = Logger.getLogger(JFrameProxy.class);
    private ProxyTableModel model;
    private MyProxy myProxy;
    private int index=-1;
    private DocumentationProvider documentation=DocumentationProvider.getInstance();
    
    /** Creates new form JFrameProxy */
    public JFrameProxy(ProxyTableModel proxyModel) {
        this.model=proxyModel;
        myProxy=new MyProxy();
        log.debug("Add new proxy");
        initComponents();
    }
    
    public JFrameProxy(ProxyTableModel proxyModel, int index) {
        this.model=proxyModel;
        this.index=index;
        myProxy=new MyProxy(proxyModel.getRow(index));
        log.debug("Edit proxy #"+index);
        initComponents();
    }
        
    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    // <editor-fold defaultstate="collapsed" desc=" Generated Code ">//GEN-BEGIN:initComponents
    private void initComponents() {
        jLabelId = new javax.swing.JLabel();
        jLabelHost = new javax.swing.JLabel();
        jLabelPort = new javax.swing.JLabel();
        jLabelUsername = new javax.swing.JLabel();
        jLabelPassword = new javax.swing.JLabel();
        jCheckBoxActive = new javax.swing.JCheckBox();
        jLabelprotocol = new javax.swing.JLabel();
        jLabelNonProxyHosts = new javax.swing.JLabel();
        jScrollPane2 = new javax.swing.JScrollPane();
        jTextAreaNonProxyHosts = new javax.swing.JTextArea();
        jTextFieldHost = new javax.swing.JTextField();
        jTextFieldId = new javax.swing.JTextField();
        jComboBoxProtocol = new javax.swing.JComboBox();
        jTextFieldUsername = new javax.swing.JTextField();
        jPasswordFieldPassword = new javax.swing.JPasswordField();
        jButtonOK = new javax.swing.JButton();
        jButtonCancel = new javax.swing.JButton();
        jSpinnerPort = new javax.swing.JSpinner();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        jLabelId.setText("id");

        jLabelHost.setText("host");

        jLabelPort.setText("port");

        jLabelUsername.setText("username");

        jLabelPassword.setText("password");

        jCheckBoxActive.setSelected(myProxy.isActive());
        jCheckBoxActive.setText("active");
        jCheckBoxActive.setToolTipText(documentation.getDescription("/Proxy/active"));
        jCheckBoxActive.setBorder(javax.swing.BorderFactory.createEmptyBorder(0, 0, 0, 0));
        jCheckBoxActive.setMargin(new java.awt.Insets(0, 0, 0, 0));

        jLabelprotocol.setText("protocol");

        jLabelNonProxyHosts.setText("non proxy hosts");

        jTextAreaNonProxyHosts.setColumns(20);
        jTextAreaNonProxyHosts.setRows(2);
        jTextAreaNonProxyHosts.setText(myProxy.getNonProxyHosts());
        jTextAreaNonProxyHosts.setToolTipText(documentation.getDescription("/Proxy/nonProxyHosts"));
        jScrollPane2.setViewportView(jTextAreaNonProxyHosts);

        jTextFieldHost.setText(myProxy.getHost());
        jTextFieldHost.setToolTipText(documentation.getDescription("/Proxy/host"));

        jTextFieldId.setText(myProxy.getId());

        jComboBoxProtocol.setModel(new javax.swing.DefaultComboBoxModel(PROTOCOLS));
        jComboBoxProtocol.setToolTipText(documentation.getDescription("/Proxy/protocol"));

        jTextFieldUsername.setText(myProxy.getUsername());
        jTextFieldUsername.setToolTipText(documentation.getDescription("/Proxy/username"));

        jPasswordFieldPassword.setText(myProxy.getPassword());
        jPasswordFieldPassword.setToolTipText(documentation.getDescription("/Proxy/password"));

        jButtonOK.setText("OK");
        jButtonOK.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButtonOKActionPerformed(evt);
            }
        });

        jButtonCancel.setText("Cancel");
        jButtonCancel.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButtonCancelActionPerformed(evt);
            }
        });

        jSpinnerPort.setModel(new SpinnerNumberModel(0,0,16384,1));
        jSpinnerPort.setToolTipText(documentation.getDescription("/Proxy/port"));
        jSpinnerPort.setValue(myProxy.getPort());
        jSpinnerPort.setEditor(new JSpinner.NumberEditor(jSpinnerPort,"####0"));

        org.jdesktop.layout.GroupLayout layout = new org.jdesktop.layout.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(layout.createSequentialGroup()
                .addContainerGap()
                .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
                    .add(org.jdesktop.layout.GroupLayout.TRAILING, layout.createSequentialGroup()
                        .add(jButtonCancel)
                        .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED, 131, Short.MAX_VALUE)
                        .add(jButtonOK))
                    .add(jScrollPane2, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 259, Short.MAX_VALUE)
                    .add(layout.createSequentialGroup()
                        .add(jLabelUsername)
                        .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                        .add(jTextFieldUsername, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 186, Short.MAX_VALUE))
                    .add(layout.createSequentialGroup()
                        .add(jLabelPassword)
                        .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                        .add(jPasswordFieldPassword, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 186, Short.MAX_VALUE))
                    .add(jLabelNonProxyHosts)
                    .add(layout.createSequentialGroup()
                        .add(jLabelHost)
                        .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                        .add(jTextFieldHost, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 219, Short.MAX_VALUE))
                    .add(org.jdesktop.layout.GroupLayout.TRAILING, layout.createSequentialGroup()
                        .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
                            .add(layout.createSequentialGroup()
                                .add(jLabelId)
                                .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                                .add(jTextFieldId, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 165, Short.MAX_VALUE)
                                .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED))
                            .add(layout.createSequentialGroup()
                                .add(jLabelprotocol)
                                .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                                .add(jComboBoxProtocol, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 60, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                                .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED, 27, Short.MAX_VALUE)
                                .add(jLabelPort)
                                .add(12, 12, 12)))
                        .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                        .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.TRAILING)
                            .add(jCheckBoxActive)
                            .add(jSpinnerPort, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 70, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(layout.createSequentialGroup()
                .addContainerGap()
                .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE)
                    .add(jLabelId)
                    .add(jCheckBoxActive)
                    .add(jTextFieldId, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE)
                    .add(jLabelprotocol)
                    .add(jComboBoxProtocol, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                    .add(jSpinnerPort, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                    .add(jLabelPort))
                .add(11, 11, 11)
                .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE)
                    .add(jLabelHost)
                    .add(jTextFieldHost, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE)
                    .add(jLabelUsername)
                    .add(jTextFieldUsername, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE)
                    .add(jLabelPassword)
                    .add(jPasswordFieldPassword, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                .add(jLabelNonProxyHosts)
                .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                .add(jScrollPane2, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED, 47, Short.MAX_VALUE)
                .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE)
                    .add(jButtonOK)
                    .add(jButtonCancel))
                .addContainerGap())
        );
        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void jButtonCancelActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButtonCancelActionPerformed
        dispose();
    }//GEN-LAST:event_jButtonCancelActionPerformed

    private void jButtonOKActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButtonOKActionPerformed
        myProxy.setActive(jCheckBoxActive.isSelected());
        myProxy.setHost(jTextFieldHost.getText());
        myProxy.setId(jTextFieldId.getText());
        myProxy.setPassword(jPasswordFieldPassword.getPassword());
        if (jSpinnerPort.getModel() instanceof SpinnerNumberModel) {
            myProxy.setPort(((SpinnerNumberModel)jSpinnerPort.getModel()).getNumber().intValue());
        }
        
        myProxy.setProtocol(PROTOCOLS[jComboBoxProtocol.getSelectedIndex()]);
        myProxy.setUsername(jTextFieldUsername.getText());
        if(index<0) {
            log.debug("new proxy added");
            model.addRow(myProxy.getProxy());           
        } else {
            log.debug("Server configuration changed");
            model.fireChange(index);
        }
        dispose();
    }//GEN-LAST:event_jButtonOKActionPerformed
    

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton jButtonCancel;
    private javax.swing.JButton jButtonOK;
    private javax.swing.JCheckBox jCheckBoxActive;
    private javax.swing.JComboBox jComboBoxProtocol;
    private javax.swing.JLabel jLabelHost;
    private javax.swing.JLabel jLabelId;
    private javax.swing.JLabel jLabelNonProxyHosts;
    private javax.swing.JLabel jLabelPassword;
    private javax.swing.JLabel jLabelPort;
    private javax.swing.JLabel jLabelUsername;
    private javax.swing.JLabel jLabelprotocol;
    private javax.swing.JPasswordField jPasswordFieldPassword;
    private javax.swing.JScrollPane jScrollPane2;
    private javax.swing.JSpinner jSpinnerPort;
    private javax.swing.JTextArea jTextAreaNonProxyHosts;
    private javax.swing.JTextField jTextFieldHost;
    private javax.swing.JTextField jTextFieldId;
    private javax.swing.JTextField jTextFieldUsername;
    // End of variables declaration//GEN-END:variables
    
}
